package com.zdkj.subway.check.controller.subwayRat.TVMRevenueVarianceAudit.TvmChaYiDXC;

import com.zdkj.subway.check.controller.CommonController;
import com.zdkj.subway.check.entity.Page;
import com.zdkj.subway.check.entity.SwAuTvmDailyBillAssist;
import com.zdkj.subway.check.entity.SwAuTvmDailyBillAssistDetail;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuTvmDailyBillAssistDetailMapper;
import com.zdkj.subway.check.mapper.SwAuTvmDailyBillAssistMapper;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.util.UserInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * tvm结账单据
 */
@Slf4j
@RestController
@RequestMapping("/checkNew")
public class TvmJZDJCtrl {

    @Resource
    SwAuTvmDailyBillAssistMapper swAuTvmDailyBillAssistMapper;
    @Resource
    SwAuTvmDailyBillAssistDetailMapper swAuTvmDailyBillAssistDetailMapper;
    @Autowired
    private CommonController commonController;

    /**
     * 结账单据协查
     * @param page
     * @param swAuTvmMoneyAssist
     * @return
     */
    @RequestMapping("/get_xw_SwAuTvmDailyBillAssistList")
    @ApiOperation(value = "结账单据协查", notes = "结账单据协查")
    public Page get_xw_SwAuTvmDailyBillAssistList(Page page, SwAuTvmDailyBillAssist swAuTvmMoneyAssist, @RequestParam Map<String,String> param){
        param.put("assistInvestStatusList","2,3");
        List<Map<String, Object>> res=swAuTvmDailyBillAssistMapper.getSwAuTvmDailyBillAssistList(param);
        page.setData(res,true);
        return page;
    }

    /**
     * 结账单据协查
     * @param swAuTvmDailyBillAssistDetail
     * @param swAuTvmDailyBillAssist
     * @param cid
     * @param crid
     * @return
     */
    @RequestMapping("/editor_xw_SwAuTvmDailyBillAssistDetail")
    @ApiOperation(value = "结账单据协查", notes = "结账单据协查")
    public Result editor_xw_SwAuTvmDailyBillAssistDetail(SwAuTvmDailyBillAssistDetail swAuTvmDailyBillAssistDetail,SwAuTvmDailyBillAssist swAuTvmDailyBillAssist,String cid,String crid){
        Result result=new Result(ResultDetailMsg.OK);
        try {
            swAuTvmDailyBillAssistDetail.setId(Integer.valueOf(cid));
            swAuTvmDailyBillAssistDetail.setRelationid(Integer.valueOf(crid));
            int i = swAuTvmDailyBillAssistDetailMapper.updateById(swAuTvmDailyBillAssistDetail);
            swAuTvmDailyBillAssist.setOperatorCode(UserInfo.getUserCode());
            swAuTvmDailyBillAssist.setOperatorId(UserInfo.getId());
            swAuTvmDailyBillAssistMapper.updateById(swAuTvmDailyBillAssist);
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_SELECT_ERROR);
            result.appendMsg("修改结账明细表出错！");
        }
        return result;
    }

    /**
     * 获取结账单详情信息
     * @param page
     * @param swAuTvmMoneyAssist
     * @return
     */
    @RequestMapping("/get_xw_SwAuTvmDailyBillAssistDetail")
    @ApiOperation(value = "获取结账单详情信息", notes = "获取结账单详情信息")
    public Result get_xw_SwAuTvmDailyBillAssistDetail(Page page, SwAuTvmDailyBillAssistDetail swAuTvmMoneyAssist){
        Result result=new Result(ResultDetailMsg.OK);
        try {
            List<Map<String,Object>> res=swAuTvmDailyBillAssistDetailMapper.getSwAuTvmDailyBillAssistDetail(swAuTvmMoneyAssist);
            result.setData(res);
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_SELECT_ERROR);
            result.appendMsg("查询结账明细表出错！");
        }
        return result;
    }


    /**
     * 提交功能，修改回馈状态
     * @param
     * @param param
     * @return
     */
    @RequestMapping("/submitData_xw_SwAuTvmDailyBillAssistDetail")
    @ApiOperation(value = "提交功能，修改回馈状态", notes = "提交功能，修改回馈状态")
    @Transactional(rollbackFor = Exception.class)
    public Result submitData_xw_SwAuTvmDailyBillAssistDetail(@RequestBody Map<String,Object> param){
        String ids = String.valueOf(param.get("ids"));
        Result result=new Result(ResultDetailMsg.OK);
        try {
            for(String id:ids.split(",")){

                SwAuTvmDailyBillAssist swAuTvmDailyBillAssist=new SwAuTvmDailyBillAssist();
                swAuTvmDailyBillAssist.setId(Integer.parseInt(id));
                swAuTvmDailyBillAssist.setReplyStatus("1");
                swAuTvmDailyBillAssist.setReplyTime(new Date());
                swAuTvmDailyBillAssist.setOperatorId(UserInfo.getId());
                swAuTvmDailyBillAssist.setOperatorCode(UserInfo.getUserCode());
                swAuTvmDailyBillAssist.setAssistInvestStatus("3");
                int i = swAuTvmDailyBillAssistMapper.updateById(swAuTvmDailyBillAssist);
            }
            //加入代办
            commonController.innerBatch(param);
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_SELECT_ERROR);
            result.appendMsg("提交出错！");
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

}
